/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.menu.service;

import java.util.ArrayList;
import java.util.List;

import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.menu.entity.OrderGoods3;
import com.thinkgem.jeesite.modules.shop.entity.Order;
import com.thinkgem.jeesite.modules.shop.entity.OrderGoods;
import com.thinkgem.jeesite.modules.shop.service.OrderGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.menu.entity.Order3;
import com.thinkgem.jeesite.modules.menu.dao.Order3Dao;

import static com.thinkgem.jeesite.common.persistence.BaseEntity.DEL_FLAG_DELETE;
import static com.thinkgem.jeesite.common.persistence.BaseEntity.DEL_FLAG_NORMAL;

/**
 * 订单Service
 * @author dby
 * @version 2020-07-12
 */
@Service
@Transactional(readOnly = true)
public class Order3Service extends CrudService<Order3Dao, Order3> {

	@Autowired
	private OrderGoods3Service orderGoods3Service;

	public Order3 get(String id) {
		return super.get(id);
	}
	
	public List<Order3> findList(Order3 order3) {
		return super.findList(order3);
	}
	
	public Page<Order3> findPage(Page<Order3> page, Order3 order3) {
		return super.findPage(page, order3);
	}
	
	@Transactional(readOnly = false)
	public void save(Order3 order3) {
		super.save(order3);
	}

	/*
	 * @Author Zhang Xingliang
	 * @Description 下单 同时保存多个订单商品
	 * @Date 15:07 2020/7/12
	 **/
	@Transactional(readOnly = false)
	public void saveOrder(Order3 order3) throws Exception{
		super.save(order3);

		List<OrderGoods3> order3GoodsList = order3.getOrderGoodsList();
		if(null == order3GoodsList || order3GoodsList.isEmpty()){
			throw new Exception("缺少订单商品！");
		}

		List<OrderGoods3> OrderGoods3InsertList = new ArrayList<>();

		List<OrderGoods3> OrderGoods3UpdateList = new ArrayList<>();

		for (OrderGoods3 orderGoods3 :order3.getOrderGoodsList()) {
			if(orderGoods3.getIsNewRecord()){
				orderGoods3.preInsert();
				OrderGoods3InsertList.add(orderGoods3);

			}else{
				orderGoods3.preUpdate();
//				orderGoods3Service.save(orderGoods3);
				OrderGoods3UpdateList.add(orderGoods3);
			}
			/*if(DEL_FLAG_DELETE.equals(orderGoods3.getDelFlag())){
				orderGoods3.preUpdate();
			}*/
			orderGoods3.setOrderId(order3.getId());
		}
		//保存多个订单商品
		orderGoods3Service.insertList(OrderGoods3InsertList);
		//批量更新
		orderGoods3Service.updateList(OrderGoods3UpdateList);
	}
	
	@Transactional(readOnly = false)
	public void delete(Order3 order3) {
		super.delete(order3);
	}


	@Autowired
	private OrderGoodsService orderGoodsService;

}